package com.antis.olsl.magic.report.quarterly;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.base.BaseFragment;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.databinding.FragmentQuarterlyBinding;
import com.antis.olsl.magic.response.GetSalesSummaryResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * 销售报表-季报-Fragment
 */
public class QuarterlyFragment extends BaseFragment<FragmentQuarterlyBinding> implements QuarterlyContract.View {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";

    private GetSalesSummaryResp.ContentBean mBean;

    private QuarterlyPresenter mPresenter;

    private boolean isLoadData = false;//数据是否加载

    private String mSalesRoomId;
    private int mStartYear;
    private int mStartQuarter;

    public QuarterlyFragment() {
    }

    public static QuarterlyFragment newInstance(String param1, int param2, int param3) {
        QuarterlyFragment fragment = new QuarterlyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM2, param2);
        args.putInt(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSalesRoomId = getArguments().getString(ARG_PARAM1);
            mStartYear = getArguments().getInt(ARG_PARAM2);
            mStartQuarter = getArguments().getInt(ARG_PARAM3);
        }
    }

    @Override
    protected FragmentQuarterlyBinding getViewBinding() {
        return FragmentQuarterlyBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initView(View view) {
        initPresenter();
        Timber.tag("hhh").e("quarterly initView");
        viewBinding.textDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = "";
                if (null != mBean) {
                    name = mBean.salesroom_name;
                }
                ARouter.getInstance()
                        .build(MagicRoutePath.ACTIVITY_QUARTERLY_DETAIL)
                        .withString("salesRoomId", mSalesRoomId)
                        .withInt("searchDate", mStartYear)
                        .withInt("searchQuarter", mStartQuarter)
                        .withString("salesRoomName", name)
                        .navigation();
            }
        });
    }

    private void initPresenter() {
        mPresenter = new QuarterlyPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void loadData() {
        loadData(mSalesRoomId, mStartYear, mStartQuarter);
        Timber.tag("hhh").e("quarterly loadData");
    }

    public void loadData(String salesroomId, int searchDate, int startQuarter) {
        if (!isLoadData) {
            mSalesRoomId = salesroomId;
            mStartYear = searchDate;
            mStartQuarter = startQuarter;
            isLoadData = true;

            Map<String, Object> map = new HashMap<>(3);
            map.put("shopId", salesroomId);
            map.put("searchTime", searchDate);
            map.put("quarter", startQuarter);
            mPresenter.getSeasonSalesSummary(map);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    public void getSeasonSalesSummarySuccess(GetSalesSummaryResp.ContentBean bean) {
        isLoadData = false;
        if (null != bean) {
            mBean = bean;

            float quarterlyProgress = 0f;
            if (!TextUtils.isEmpty(bean.jmbdcl)) {
                try {
                    quarterlyProgress = Float.parseFloat(bean.jmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.quarterlySeekBar.setProgress(quarterlyProgress);
            float yearProgress = 0f;
            if (!TextUtils.isEmpty(bean.nmbdcl)) {
                try {
                    yearProgress = Float.parseFloat(bean.nmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.yearSeekBar.setProgress(yearProgress);

            setSalesAmountValue(bean);
            setSalesVolumeValue(bean);
        }
    }

    private void setSalesAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xsje)) {
            viewBinding.layoutSalesAmount.textSalesAmount.setText(bean.xsje);
        } else {
            viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsjezb)) {
            String salesProportion = bean.xsjezb + "%";
            viewBinding.layoutSalesAmount.textSalesProportion.setText(salesProportion);
        } else {
            viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbxsje)) {
            String salesMom = bean.hbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesMom.setText(salesMom);
        } else {
            viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxsje)) {
            String salesYoy = bean.tbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesYoy.setText(salesYoy);
        } else {
            viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        }
    }

    private void setSalesVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xssl)) {
            viewBinding.layoutSalesVolume.textSalesVolume.setText(bean.xssl);
        } else {
            viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsslzb)) {
            String volumeProportion = bean.xsslzb + "%";
            viewBinding.layoutSalesVolume.textVolumeProportion.setText(volumeProportion);
        } else {
            viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hb_xssl)) {
            String volumeMom = bean.hb_xssl + "%";
            viewBinding.layoutSalesVolume.textVolumeMom.setText(volumeMom);
        } else {
            viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxssl)) {
            String volumeYoy = bean.tbxssl + "%";
            viewBinding.layoutSalesVolume.textVolumeYoy.setText(volumeYoy);
        } else {
            viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        }
    }

    @Override
    public void getSeasonSalesSummaryFailure(String msg) {
        isLoadData = false;
        Timber.tag("hhh").e("getSeasonSalesSummaryFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(getContext(), msg);
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.quarterlySeekBar.setProgress(0);
        viewBinding.yearSeekBar.setProgress(0);
        viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
    }

    @Override
    public void getSeasonSalesSummaryEmpty() {
        isLoadData = false;
        Timber.tag("hhh").e("getSeasonSalesSummaryEmpty");
        setDefaultValue();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

}
